using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using BIUtilities.Publish.CustomConfiguration;

namespace BIUtilities.ReportPublisher
{
    public static class ParameterReplacement
    {
        public static void UpdateConfigWithReplacements(ConfigurationElement deployConfig, CommandLineParams paramData)
        {
            if (paramData.ReplacementList.Count > 0)
            {
                PerformSectionReplacements(deployConfig, paramData);

                Trace.WriteLine("Parameter Replacement Statistics:");
                Trace.Indent();
                foreach (string findParam in paramData.ReplacementList.Keys)
                {
                    string traceString =
                        string.Format("{0} = {1} (replaced {2} times)",
                        findParam,
                        paramData.ReplacementList[findParam],
                        paramData.ReplacementCount[findParam]);
                    Trace.WriteLine(traceString);
                }
            }
        }

        /// <summary>
        /// Apply command line replacements
        /// </summary>
        /// <param name="deployConfig"></param>
        /// <param name="dictionary"></param>
        public static void PerformSectionReplacements(
            ConfigurationElement cfgSection, CommandLineParams paramData)
        {
            foreach (PropertyInformation propInfo in cfgSection.ElementInformation.Properties)
            {
                IUpdatableConfig updCfg = (IUpdatableConfig)cfgSection;
                if (propInfo.Type == typeof(string))
                {
                    string value = (string)updCfg[propInfo.Name];
                    foreach (string findParam in paramData.ReplacementList.Keys)
                    {
                        if (value.IndexOf(findParam) != -1)
                        {
                            value = value.Replace("[" + findParam + "]", paramData.ReplacementList[findParam]);
                            paramData.ReplacementCount[findParam] = paramData.ReplacementCount[findParam] + 1;
                        }
                    }
                    cfgSection.LockItem = true;
                    updCfg[propInfo.Name] = value;
                    cfgSection.LockItem = false;
                }
                else if ((propInfo.Type.IsSubclassOf(typeof(ConfigurationElementCollection))) || 
                    (propInfo.Type.IsSubclassOf(typeof(ConfigurationElement))))
                {
                    ConfigurationElement element = updCfg[propInfo.Name] as ConfigurationElement;
                    if (element != null)
                    {
                        PerformSectionReplacements(element, paramData);
                    }
                }
            }

            if (cfgSection is ConfigurationElementCollection)
            {
                ConfigurationElementCollection elementCollection = cfgSection as ConfigurationElementCollection;
                foreach (ConfigurationElement element in elementCollection)
                {
                    PerformSectionReplacements(element, paramData);
                }
            }
        }
    }
}
